package com.woniuxy.yoga.course.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.course.model.dto.alone.AloneCourseDTO;
import com.woniuxy.yoga.course.model.form.alone.AddAloneCourseForm;
import com.woniuxy.yoga.course.model.form.alone.AmountAloneCourseDurationForm;
import com.woniuxy.yoga.course.model.form.alone.CoachQueryAloneCourseForm;
import com.woniuxy.yoga.course.model.form.alone.QueryAllAloneCourseForm;
import com.woniuxy.yoga.course.model.param.alone.AddAloneCourseParam;
import com.woniuxy.yoga.course.model.param.alone.AmountAloneCourseDurationParam;
import com.woniuxy.yoga.course.model.param.alone.CoachQueryAloneCourseParam;
import com.woniuxy.yoga.course.model.param.alone.QueryAllAloneCourseParam;
import com.woniuxy.yoga.course.service.service.AloneCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 私教课程表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/aloneCourse")
@Api(tags = "私教课程相关业务接口")
@Validated
public class AloneCourseController {

    @Autowired
    private AloneCourseService aloneCourseService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 教练ID查询私教课程
     * @param ids
     * @return
     */
    @PostMapping("/getByCoachId")
    @ApiOperation("教练ID查询私教课程接口")
    public Result<List<AloneCourseDTO>> getByCoachId(@RequestBody List<Integer> ids){
        //获取数据
        List<AloneCourseDTO> aloneCourseDTOS = aloneCourseService.getByCoachId(ids);
        //返回数据
        return Result.getListResult(AloneCourseDTO.class).setMessage("根据教练ID查询私教课程信息成功").setData(aloneCourseDTOS);
    }

    /**
     * 统计该时间段私教课程已完成的总时长
     * @param amountAloneCourseDurationForm
     * @return
     */
    @PostMapping("/amountAloneCourseDuration")
    @ApiOperation("统计私教课程总时长接口")
    public Result<Integer> amountAloneCourseDuration(@RequestBody AmountAloneCourseDurationForm amountAloneCourseDurationForm){
        Integer duration = aloneCourseService.amountAloneCourseDuration(BeanUtil.toBean(amountAloneCourseDurationForm, AmountAloneCourseDurationParam.class));
        return Result.getResult(Integer.class).setMessage("统计该时间段私教课程已完成的总时长成功").setData(duration);
    }

    /**
     * 新增私教课程
     * @param addAloneCourseForm
     * @return
     */
    @PostMapping("/addAloneCourse.co")
    @ApiOperation("新增私教课程接口")
    public Result<?> addAloneCourse(@Valid @RequestBody AddAloneCourseForm addAloneCourseForm){
        aloneCourseService.addAloneCourse(BeanUtil.toBean(addAloneCourseForm, AddAloneCourseParam.class));
        return Result.getResult().setMessage("新增私教课程成功");
    }

    /**
     * 修改成已完成
     * @param courseId
     * @return
     */
    @GetMapping("/modifyAloneCourseComplete/{courseId}.co")
    @ApiOperation("修改完成接口")
    @ApiImplicitParam(name = "courseId",value = "私教课程ID")
    public Result<?> modifyAloneCourseComplete(@Valid @NotNull(message = "私教课程ID不能为空") @PathVariable("courseId") Integer courseId){
        aloneCourseService.modifyAloneCourseComplete(courseId);
        return Result.getResult().setMessage("修改成已完成成功");
    }

    /**
     * 教练查询自己的私教课程
     * @param coachQueryAloneCourseForm
     * @return
     */
    @PostMapping("/coachQueryAloneCourse.co")
    @ApiOperation("教练查询自己的私教课程接口")
    public Result<PageInfo> coachQueryAloneCourse(@RequestBody CoachQueryAloneCourseForm coachQueryAloneCourseForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析token 获取coachId
        Integer coachId = JWTUtil.getCoachId(refreshToken);
        //对象转换
        CoachQueryAloneCourseParam param = BeanUtil.toBean(coachQueryAloneCourseForm, CoachQueryAloneCourseParam.class);
        //赋值教练ID
        param.setCoachId(coachId);
        //返回数据
        PageInfo pageInfo = aloneCourseService.coachQueryAloneCourse(param);
        return Result.getResult(PageInfo.class).setMessage("教练查询自己的私教课程成功").setData(pageInfo);
    }

    /**
     * 商家查询自己的私教课程
     * @param queryAllAloneCourseForm
     * @return
     */
    @PostMapping("/queryAllAloneCourse.bu")
    @ApiOperation("商家查询自己的私教课程接口")
    public Result<PageInfo> queryAllAloneCourse(@Valid @RequestBody QueryAllAloneCourseForm queryAllAloneCourseForm){
        PageInfo pageInfo = aloneCourseService.queryAllAloneCourse(BeanUtil.toBean(queryAllAloneCourseForm, QueryAllAloneCourseParam.class));
        return Result.getResult(PageInfo.class).setMessage("商家查询自己的私教课程成功").setData(pageInfo);
    }
}

